<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Services;

use App\Models\Common\Category;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CategoryService
{
    use Service;

    /**
     * 博客新分类增展示
     *
     * @param Request $request
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function CategoryService( $request )
    {
        $where  = function ( $query ) use ( $request ) {
            $query->where('status', 1);
            $query->where('static', $request->input('static', 1));
            $query->where('parent_id', 0);
        };
        $with   = [
            'children' => function ( $query ) {//二级分类
                $query->select('id', 'parent_id', 'name', 'sort_order', 'is_show', 'status', 'static', 'created_at');
                $query->orderBy('sort_order')->orderBy('created_at', 'desc');
            },
        ];
        $select = [ 'id', 'parent_id', 'name', 'sort_order', 'is_show', 'status', 'static', 'created_at' ];
        return Category::GetModel($where, $with, $select);
    }

    /**
     * 博客分类新增
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function CategoryStoreService( $request )
    {
        $validator = $this->getAdminValidator()->CategoryValidator($request);
        if ( $validator->first() ) return back()->with('error',$validator->first());
        Category::CreateModel($request->all());
        return back()->with('success', $this->getErrorCode()::message(209));
    }

    /**
     * 博客分类编辑页面
     *
     * @param $id
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function CategoryEditService( $id )
    {
        $select = [ 'id', 'parent_id', 'is_show', 'sort_order', 'name', 'status', 'static', ];
        $cate   = Category::FindModel($id, [], $select);
        return view('blog.category.edit', compact('cate'));
    }

    /**
     * 博客分类更新
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function CategoryUpdateService( $request )
    {
        $select    = [ 'id', 'name', 'is_show', 'parent_id', 'sort_order', 'status', 'static' ];
        $validator = $this->getAdminValidator()->CategoryValidator($request);
        if ( $validator->first() ) return back()->with('error',$validator->first());
        $category = Category::FindModel($request->input('id'), [], $select);
        $category->update($request->all());
        return back()->with('success', $this->getErrorCode()::message(203));
    }

    /**
     * 博客分类排序
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function CategorySortOrderService( $request )
    {
        $array = json_decode($request->input('sort'), TRUE);
        if ( ! ( new CommonService() )->sort($array, 'category', 0, 3, 1) ) return message($this->getErrorCode()::message(2196));
        return data();
    }

    /**
     * 博客分类删除
     *
     * @param $id
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function CategoryDestroyService( $id )
    {
        $error = $this->getErrorCode();
        if ( ! Category::CheckChildren($id) ) return back()->with('error',$error::message(1025));
        Category::DestroyModel($id);
        return back()->with('success', $error::message(204));
    }

    /**
     * 修改博客分类是否显示
     *
     * @param $id
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function CategoryStatusService( $id )
    {
        $select   = [ 'parent_id', 'id', 'is_show' ];
        $with     = [
            'children' => function ( $query ) use ( $select ) {
                $query->select($select);
            },
        ];
        $category = Category::FindModel($id, $with, $select);
        if ( $category->is_show == 1 ) $is_show = 2;
        else $is_show = 1;
        DB::transaction(function () use ( $id, $is_show, $category ) {
            if ( $category->children->isEmpty() ) foreach ( $category->children as $value ) {
                $value->is_show = $is_show;
                $value->save();
            }
            $category->is_show = $is_show;
            $category->save();
        });
        return back()->with('success', $this->getErrorCode()::message(210));
    }

}
